package com.gzt.bookcatalog.dto;

import com.gzt.bookcatalog.constant.ResultCodeConstant;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * 响应结果
 *
 * @author 38014
 * @date 2025-05-22 07:26:02
 */
@Data
public class RestResult<T> {

    /**
     * 业务返回码
     */
    @Schema(description = "业务返回码")
    private Integer code;

    /**
     * 业务提示信息
     */
    @Schema(description = "业务提示信息")
    private String msg;

    /**
     * 业务数据
     */
    @Schema(description = "业务数据")
    private T data;

    public static <T> RestResult<T> success(T data) {
        RestResult<T> r = new RestResult<>();
        r.setCode(0);
        r.setData(data);
        return r;
    }

    public static <T> RestResult<T> error(Integer code,T data) {
        RestResult<T> r = new RestResult<>();
        r.setCode(code);
        r.setData(data);
        return r;
    }

    public static <T> RestResult<T> error(String message) {
        RestResult<T> r = new RestResult<>();
        r.setCode(ResultCodeConstant.CODE_000001);
        r.setMsg(message);
        r.setData(null);
        return r;
    }

    public static <T> RestResult<T> error(Integer code,String message) {
        RestResult<T> r = new RestResult<>();
        r.setCode(code);
        r.setMsg(message);
        r.setData(null);
        return r;
    }

    public RestResult() {
    }

    public boolean isSuccess() {
        return this.code == 0;
    }



}
